import 'package:elevator_app/config/global_config.dart';
import 'package:flutter/material.dart';

import '../../../controller/platform/devices_typing.dart';
import '../../../controller/platform/platform_ctrl.dart';

class TextFieldWidget {
  static bool _isChange = false;

  // 上下文
  static BuildContext ct;

  // 用户名、密码输入框的控制器
  static TextEditingController account = TextEditingController();
  static TextEditingController password = TextEditingController();

  static final _accountFocus = FocusNode();
  static final _passwordFocus = FocusNode();

  /// 构建用户名，密码输入框
  /// * [context] 上下文
  /// * [loginInfo] 登录信息
  static Widget build(BuildContext context, Map info) {
    // --
    String account_a;

    // 存储上下文
    ct = context;
    // 登录信息
    account_a = !_isChange
        ? info["account"].isEmpty
            ? account_a
            : info["account"]
        : account_a;
    password.text = !_isChange
        ? info["password"].isEmpty
            ? password.text
            : info["password"]
        : password.text;

    account.value = TextEditingValue(
      text: account_a,
      selection: TextSelection.fromPosition(TextPosition(
          affinity: TextAffinity.downstream, offset: account_a.length)),
    );

    return Column(
      children: PlatformCtrl.runPlatform == SystemInfo.android
          ? _structureAndroidWidget()
          : _structureIosWidget(),
    );
  }

  // 针对android绘制输入框并添加行为处理
  static List<Widget> _structureAndroidWidget() {
    List<Widget> lw = [];
    lw = [
      Container(
        child: Listener(
          onPointerDown: (e) => FocusScope.of(ct).requestFocus(_accountFocus),
          child: TextField(
            controller: account,
            focusNode: _accountFocus,
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            keyboardAppearance: Brightness.light,
            cursorColor: GlobalConfig.theme,
            cursorWidth: 1,
            decoration: InputDecoration(
              hintText: '请输入账户',
              filled: true,
              fillColor: Color.fromRGBO(255, 255, 255, .8),
              hintStyle: TextStyle(
                color: GlobalConfig.assistColor,
              ),
              prefixIcon: Icon(
                Icons.account_box,
                color: GlobalConfig.assistColor,
              ),
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.borderColor),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.theme),
              ),
            ),
            onChanged: (String v) {
              if (!_isChange) _isChange = true;

//              TextEditingValue(
//                text: v,
//                selection: TextSelection.fromPosition(
//                  TextPosition(
//                    affinity: TextAffinity.downstream,
//                    offset: v.length,
//                  ),
//                ),
//              );
            },
          ),
        ),
      ),
      Container(
        child: Listener(
          onPointerDown: (e) => FocusScope.of(ct).requestFocus(_passwordFocus),
          child: TextField(
            controller: password,
            focusNode: _passwordFocus,
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            keyboardAppearance: Brightness.light,
            cursorColor: GlobalConfig.theme,
            cursorWidth: 1,
            decoration: InputDecoration(
              hintText: '请输入密码',
              filled: true,
              fillColor: Color.fromRGBO(255, 255, 255, .8),
              hintStyle: TextStyle(
                color: GlobalConfig.assistColor,
              ),
              prefixIcon: Icon(
                Icons.keyboard,
                color: GlobalConfig.assistColor,
              ),
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.borderColor),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: GlobalConfig.theme),
              ),
            ),
            obscureText: true,
            onChanged: (String v) {
              if (!_isChange) _isChange = true;
            },
          ),
        ),
      )
    ];

    return lw;
  }

  // 针对ios绘制输入框并添加行为处理
  static List<Widget> _structureIosWidget() {
    List<Widget> lw = [];
    lw = [
      Container(
        child: TextField(
          controller: account,
          focusNode: _accountFocus,
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.done,
          keyboardAppearance: Brightness.light,
          decoration: InputDecoration(
            hintText: '请输入账户',
            prefixIcon: Icon(Icons.account_box),
          ),
          onChanged: (String v) {
            if (!_isChange) _isChange = true;
          },
        ),
      ),
      Container(
        child: TextField(
          controller: password,
          focusNode: _passwordFocus,
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.done,
          keyboardAppearance: Brightness.light,
          decoration: InputDecoration(
            hintText: '请输入密码',
            prefixIcon: Icon(Icons.keyboard),
          ),
          obscureText: true,
          onChanged: (String v) {
            if (!_isChange) _isChange = true;
          },
        ),
      ),
    ];
    return lw;
  }

  // -
  static Map verificaLoginInfo() {
    // 验证用户名
    // !regExp.hasMatch(account.text)
    if (account.text == null || account.text.trim().length < 5) {
      return {"state": false, "msg": "用户名输入有误"};
    }

    // 验证密码
    if (password.text == null || password.text.length <= 5) {
      return {"state": false, "msg": "密码输入有误"};
    }

    return {
      "state": true,
      "data": {
        "account": account.text,
        "password": password.text,
      },
      "msg": "通过"
    };
  }
}
